﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.PQ
{
    public partial class ht_comp_edit : PageBase
    {
        public string whe = "", path = "", tab = "pq_ht";
        public string us, um, ubh;   
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();


            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["sid"]))
                {
                     hidsid.Text = Request["sid"].Trim();                
                }
                fill();
                fill2();
                fill3();
                Regcomp();
            }
            else
            {
                fillcomp();                
            }
                  

        
        }


        void Regcomp()
        {
            ExtAspNet.PageContext.RegisterStartupScript
                           (
                           "var d=Ext.getCmp('" + ddlcompany.ClientID + "');" +
                           " var oldInput=\"\";" +
                           " d.listWidth=500; " +
                           " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                           " var newInput=document.getElementById(\"" + ddlcompany.ClientID + "\").value;" +
                            " if (oldInput==newInput) {return;} " +
                           " oldInput=newInput;" +
                            " __doPostBack('loadcomp',newInput);" +
                            "});"
                           );
            ExtAspNet.PageContext.RegisterStartupScript
                 (
                 "var d=Ext.getCmp('" + ddlperson.ClientID + "');" +
                 " var oldInput=\"\";" +
                 " d.listWidth=500; " +
                 " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                 " var newInput=document.getElementById(\"" + ddlperson.ClientID + "\").value;" +
                  " if (oldInput==newInput) {return;} " +
                 " oldInput=newInput;" +
                  " __doPostBack('loadpepo',newInput);" +
                  "});"
                 );
    
            ExtAspNet.PageContext.RegisterStartupScript
              (
              "var d=Ext.getCmp('" + ddlzsales.ClientID + "');" +
              " var oldInput=\"\";" +
              " d.listWidth=500; " +
              " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
              " var newInput=document.getElementById(\"" + ddlzsales.ClientID + "\").value;" +
               " if (oldInput==newInput) {return;} " +
              " oldInput=newInput;" +
               " __doPostBack('loadzss',newInput);" +
               "});"
              );

        }

        void fillcomp()
        {

            if (Request["__EVENTTARGET"] == "loadcomp")
            {

                string sqltxt = "SELECT * FROM company where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',cname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlcompany.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["cname"].ToString(), dt.Rows[i]["cname"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlcompany.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

           
            if (Request["__EVENTTARGET"] == "loadpepo")
            {

                string sqltxt = "SELECT * FROM sys_user where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlperson.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlperson.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlperson.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }
         

            if (Request["__EVENTTARGET"] == "loadzss")
            {
                string sqltxt = "SELECT * FROM sys_user where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlzsales.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlzsales.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlzsales.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

        }


        //填 充ddl
        void fill()
        {
            DataTable e;

            //驻厂员工
            e = BLL.Bllbase.get_table("[sys_user]");
            ddlzsales.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzsales.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["user_name"].ToString().Trim()));
            }
            ddlzsales.DataBind();
            ddlzsales.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlzsales.SelectedIndex = ddlzsales.Items.IndexOf(ddlzsales.Items.FindByText(""));


            //公司
            e = BLL.Bllbase.get_table("[company]");
            ddlcompany.Items.Clear();          

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcompany.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));
            }
            ddlcompany.DataBind();          
            ddlcompany.Items.Add(new ExtAspNet.ListItem("", "")); 
            ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByText(""));




            //员工
            e = BLL.Bllbase.get_table("[sys_user]");
            ddlperson.Items.Clear();         

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlperson.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));

            }
            ddlperson.DataBind();
           ddlperson.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByText(""));
                   
        }
      



        void fill3()
        {
            string dts; DateTime lastday;

            //BLL.PQ.pq_ht S = new BLL.PQ.pq_ht();
            //DataTable d = S.GetList().Tables[0];

            DataTable d = BLL.Bllbase.get_table("comp_ht", "sid=" + hidsid.Text);
            hidsbh.Text = d.Rows[0]["htbh"].ToString().Trim();
            txtht.Text = d.Rows[0]["htbh"].ToString().Trim();

            ddlzsales.SelectedIndex = ddlzsales.Items.IndexOf(ddlzsales.Items.FindByText(d.Rows[0]["zsales"].ToString()));

            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByValue(d.Rows[0]["pbh"].ToString()));
            ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByText(d.Rows[0]["pvendor"].ToString()));

            string ft = d.Rows[0]["ftype"].ToString().Trim();

          
            dts = d.Rows[0]["sdate"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                date1.Text = lastday.ToString("yyyy-M-d");
            }

            dts = d.Rows[0]["edate"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                date2.Text = lastday.ToString("yyyy-M-d");
            }


            txtmoney.Text = d.Rows[0]["money"].ToString().Trim();
            txtworktype.Text = d.Rows[0]["work"].ToString().Trim();
            txtmemo.Text = d.Rows[0]["memo"].ToString().Trim();


        }

        protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = ddltype.SelectedValue;
            //公司
            DataTable se = BLL.Bllbase.get_table(type);

            ddlcompany.Items.Clear();

            if (type == "company")
            {
                for (int i = 0; i < se.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(se.Rows[i]["cname"].ToString().Trim(), se.Rows[i]["cbh"].ToString().Trim()));
                }
            }

            if (type == "school")
            {
                for (int i = 0; i < se.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(se.Rows[i]["schname"].ToString().Trim(), se.Rows[i]["schbh"].ToString().Trim()));
                }
            }

            if (type == "zj")
            {
                for (int i = 0; i < se.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(se.Rows[i]["zname"].ToString().Trim(), se.Rows[i]["zbh"].ToString().Trim()));
                }
            }
            ddlcompany.DataBind();
        }

        void fill2()
        {
            string dts; DateTime lastday;

            BLL.PQ.comp_ht S = new BLL.PQ.comp_ht();
            DataTable d=  S.GetList("sid="+hidsid.Text).Tables[0];


            hidsbh.Text = d.Rows[0]["htbh"].ToString().Trim();
            txtht.Text = d.Rows[0]["htbh"].ToString().Trim();

            ddltype.SelectedIndex = ddltype.Items.IndexOf(ddltype.Items.FindByValue(d.Rows[0]["httype"].ToString()));

            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByText(d.Rows[0]["pbh"].ToString()));

            ddlcompany.Items.Add(new ExtAspNet.ListItem(d.Rows[0]["pvendor"].ToString().Trim(), d.Rows[0]["pvbh"].ToString().Trim())); 

            //ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByText(d.Rows[0]["pvendor"].ToString()));


            dts = d.Rows[0]["sdate"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                date1.Text = lastday.ToString("yyyy-M-d");
            }

            dts = d.Rows[0]["edate"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                date2.Text = lastday.ToString("yyyy-M-d");
            }

            dts = d.Rows[0]["rdage"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                regedate.Text = lastday.ToString("yyyy-M-d");
            }
            txtnum.Text = d.Rows[0]["snum"].ToString().Trim();

            txtmoney.Text =d.Rows[0]["money"].ToString().Trim();
            txtworktype.Text = d.Rows[0]["work"].ToString().Trim();
            txtmemo.Text = d.Rows[0]["memo"].ToString().Trim();

            linknm.Text = d.Rows[0]["ftype"].ToString().Trim();   //联系人
            linktel.Text = d.Rows[0]["fbh"].ToString().Trim();  //联系电话

        }
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {             
            up_member();

            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }


 


        void up_member()
        {
        

            Model.PQ.comp_ht s = new Model.PQ.comp_ht();
            s.sid =int.Parse(hidsid.Text);
          
            s.htbh = txtht.Text;

            s.pbh = ddlperson.SelectedValue;
            s.pname = ddlperson.SelectedText;
            s.pvendor = ddlcompany.SelectedText;
            s.pvbh = ddlcompany.SelectedValue;
            s.httype = ddltype.SelectedValue;


            if (VAN.IsDateTime(date1.Text))
                s.sdate = DateTime.Parse(date1.Text);
            else
                s.sdate = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(date2.Text))
                s.edate = DateTime.Parse(date2.Text);
            else
                s.edate = DateTime.Parse("1900-01-01");

           
            if (VAN.IsDateTime(regedate.Text))
               s.rdage = DateTime.Parse(regedate.Text);
            else
               s.rdage = DateTime.Parse("1900-01-01");


            s.ftype = "";
            s.fbh = "";
            s.fname = "";
          
            s.ftype = linknm.Text;  //联系人
            s.fbh = linktel.Text;   //联系电话
            
            s.owen = "";
           
            s.zsales = "";
            s.zsales = ddlzsales.SelectedText;
            s.snum =int.Parse(txtnum.Text);

            s.money = 0;
            s.money = decimal.Parse(txtmoney.Text);

            s.work = "";
            s.work = txtworktype.Text;

            s.status = 0;

            s.memo = "";
            s.memo = txtmemo.Text;

            BLL.PQ.comp_ht t = new BLL.PQ.comp_ht();
            t.Update(s);
            string sqltxt = "update  comp_ht set  updtime=getdate(), sales='" + um + "',sbh='" + ubh + "' where 1=1 and htbh='" + s.htbh + "'";
            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
            //更新业务员操作记录
            BLL.Bllbase.update_rec(tab, um, " htbh='" + s.htbh + "' "); 
 
        }
       
    }
}